import 'dart:convert';

import 'package:du_application/http/HttpUtil.dart';
import 'package:du_application/http/Options.dart';
import 'package:du_application/model/InfoModel.dart';
import 'package:du_application/model/ResultModel.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class InfoProvider with ChangeNotifier{
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
 InfoModel infoModel;
   Address address=Address();
 
  String code1="110000";
  String code2="110100";
  String code3="110101";
 getInfo() async {
 await _prefs.then((ins) async{
  Options options =  Options(ins.getString("token")??"");
  await HttpUtil.getInstance()
        .get("/user/index",
           options: options.options )
        .then((res) async {
      infoModel=InfoModel.fromJson(json.decode(res));
      if(infoModel.errno==0){
         notifyListeners();
      }
  });
 });
  }
   updateInfo({String nickname,int gender,String avatar}) async {
 await _prefs.then((ins) async{
  Options options =  Options(ins.getString("token")??"");
  await HttpUtil.getInstance()
        .post("/auth/profile",data: {"nickname":nickname,"gender":gender,"avatar":avatar},
           options: options.options)
        .then((res) async {
       ResultModel resultModel=ResultModel.fromJson(json.decode(res));

      if(resultModel.errno==0){
       getInfo();
      }
  });
 }
 );
  }
  setSomeThing({String addressDetail=null,String tel=null,String name=null,bool isDefault=false,})  {
    if(address.province==null){
     address.province=code1;
    address.city=code2;
    address.county=code3;
    address.areaCode=code3;
    }
    if(addressDetail!=null){
 address.addressDetail=addressDetail;
    }
    if(tel!=null){
 address.tel=tel;
    }
   
    if(name!=null){
 address.name=name;
    }
   address.isDefault=isDefault;
  notifyListeners();
  }
 save() async {
 await _prefs.then((ins) async{
   Options options =  Options(ins.getString("token")??"");
  await HttpUtil.getInstance()
        .post("/address/save",data: address,
           options: options.options)
        .then((res) async {
       ResultModel resultModel=ResultModel.fromJson(json.decode(res));
      if(resultModel.errno==0){
       getInfo();
    }
  });
 });
}
  setCode(String code1,String code2,String code3){
    address.province=code1;
    address.city=code2;
    address.county=code3;
    address.areaCode=code3;
    this.code1=code1;
    this.code2=code2;
    this.code3=code3;
   notifyListeners();
  }
   updateAddress(Address newaddress) async {
 await _prefs.then((ins) async{
   Options options =  Options(ins.getString("token")??"");
  await HttpUtil.getInstance()
        .post("/address/save",data: newaddress,
           options: options.options)
        .then((res) async {
       ResultModel resultModel=ResultModel.fromJson(json.decode(res));
      if(resultModel.errno==0){
       getInfo();
    }
  });
 });
   }
  replaceCode(item){
   address=infoModel.data.address[item];
   address.isDefault=true;
   save();
  }
  delAddress(int id) async {
 await _prefs.then((ins) async{
   Options options =  Options(ins.getString("token")??"");
  await HttpUtil.getInstance()
        .post("/address/delete",data: {"id":id},
           options: options.options)
        .then((res) async {
      ResultModel resultModel=ResultModel.fromJson(json.decode(res));
      if(resultModel.errno==0){
       getInfo();
    }
  });
 });
  }

}